import {
  createRouter,
  createWebHashHistory,
  createWebHistory
} from 'vue-router'
import Home from '../views/Home.vue'
import NProgress from 'nprogress' // 进度条
import 'nprogress/nprogress.css' //样式必须引入

// 简单配置
NProgress.configure({
  easing: 'ease',
  speed: 500,
  showSpinner: false
})

const routes = [

  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import( /* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/pageError',
    name: 'pageError',
    component: () => import( /* webpackChunkName: "about" */ '../views/pageError.vue')
  },
]

const router = createRouter({

  history: createWebHashHistory(process.env.BASE_URL),
  // history: createWebHistory(process.env.BASE_URL),
  routes
})

// 全局监听路由
router.beforeEach((to, from, next) => {
  NProgress.start()

  // if (!to.name) {

  //   next('/pageError')
  //   return;

  // }


  if (to.meta && to.meta.isHTMLTitle && to.meta.title) {
    document.title = to.meta.title
  }

  next();
});

router.afterEach(() => {
  NProgress.done()
})


export default router